package pro.seac.twitter.web.view;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Component;

import pro.seac.twitter.application.UserManagementService;
import pro.seac.twitter.application.dto.UserAccountData;
import pro.seac.twitter.web.Navigateable;

import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.data.fieldgroup.PropertyId;
import com.vaadin.data.validator.EmailValidator;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Notification.Type;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

@Component
@Scope("prototype")
@Navigateable("account")
@Secured("ROLE_USER")
public class UserAccountView extends BaseView {
	private static final long serialVersionUID = 1L;
	private VerticalLayout formContainer;
	@PropertyId("name")
	private TextField userNameField;
	// @PropertyId("username")
	// private TextField accountNameField;
	@PropertyId("password")
	private PasswordField passwordField;
	@PropertyId("email")
	private TextField emailField;
	@PropertyId("avatarUrl")
	private TextField avatarUrlField;

	private UserAccountData model;
	private BeanFieldGroup<UserAccountData> binder;

	@Autowired
	private UserManagementService service;

	@Override
	public void enter(ViewChangeEvent event) {
		UserAccountData usrer = getCurrentUser();
		model = service.getUserAccountById(usrer.getId());
		// stworz ui
		initComponents();
		// binduj model z ui
		binder = new BeanFieldGroup<UserAccountData>(UserAccountData.class);
		binder.setBuffered(false);
		binder.setItemDataSource(model);
		binder.bindMemberFields(this);
	}

	@Override
	protected com.vaadin.ui.Component initContent() {
		formContainer = (VerticalLayout) super.initContent();

		formContainer.addComponent(userNameField = new TextField("Imie i nazwisko"));
		userNameField.setNullRepresentation(StringUtils.EMPTY);
		userNameField.setValidationVisible(true);
		userNameField.setRequired(true);

		// formContainer.addComponent(accountNameField = new TextField("Nazwa uzytkwnika"));
		// accountNameField.setNullRepresentation(StringUtils.EMPTY);
		// accountNameField.setValidationVisible(true);
		// accountNameField.setRequired(true);
		// accountNameField.setReadOnly(true);

		formContainer.addComponent(passwordField = new PasswordField("Haslo"));
		passwordField.setNullRepresentation(StringUtils.EMPTY);
		passwordField.setValidationVisible(true);
		passwordField.setRequired(true);

		formContainer.addComponent(emailField = new TextField("Adres email"));
		emailField.setNullRepresentation(StringUtils.EMPTY);
		emailField.setValidationVisible(true);
		emailField.setRequired(true);
		emailField.addValidator(new EmailValidator("Niepoprawny format"));

		// formContainer.addComponent(avatarUrlField = new TextField("Avatar"));
		// avatarUrlField.setNullRepresentation(StringUtils.EMPTY);
		// avatarUrlField.setRequired(false);

		HorizontalLayout actions = new HorizontalLayout();
		actions.setSpacing(true);

		Button actionButton;
		actions.addComponent(actionButton = new Button("zapisz"));
		actionButton.addClickListener(new ClickListener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				register();
			}
		});
		actions.addComponent(actionButton = new Button("anuluj"));
		actionButton.addClickListener(new ClickListener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				goToPublicHomeView();
			}
		});
		formContainer.addComponent(actions);
		return formContainer;
	}

	public void register() {
		if (binder.isValid()) {
			service.updateUserAccount(model);
			Notification.show("Sukces", "Twoje konto zosta\u0142o zaktualizowane", Type.HUMANIZED_MESSAGE);
		} else {
			Notification.show("B\u0142\u0105d", "Uzupe\u0142nij poprawnie wszystkie pola", Type.ERROR_MESSAGE);
		}
	}

	@Override
	protected String getTitle() {
		return "Edycja ustawie\u0144 konta: " + model.getUsername();
	}
}
